package pers.sports.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import pers.sports.entity.Student;
import pers.sports.service.CollegeService;
import pers.sports.service.StudentService;
import pers.sports.vo.StorageUser;

import javax.servlet.http.HttpServletRequest;

@Controller
public class IndexController {

    private final StudentService studentService;
    private final CollegeService collegeService;

    public IndexController(StudentService studentService, CollegeService collegeService) {
        this.studentService = studentService;
        this.collegeService = collegeService;
    }

    @RequestMapping("/")
    public String homepage() {
        return "homepage";
    }

    @RequestMapping(value = "/toPage", method = RequestMethod.GET)
    public String toPage(HttpServletRequest request) {

        return request.getParameter("url");
    }

    @RequestMapping("/userinfo")
    public ModelAndView userinfo(HttpServletRequest request) {
        StorageUser user = (StorageUser) request.getSession().getAttribute("user");
        ModelAndView mav = new ModelAndView("editInformation");
        Student student = studentService.findInfoByNum(user.getNum());
        mav.addObject(student);
        String college = collegeService.findNameById(student.getCollegeId().toString());
        mav.addObject("college", college);
        return mav;
    }
}